# Pre-defined packages
import pygame
import sys
import time
import random
# Custom packages
import boss
import player
import bullet
import menu
import barlines
import inkblast
import slowtime
import healthpickup
import speedtime
import note
import rest
import level
import string
import math

# MAKE LOSE AND WIN SCREENS

class Game(object):
    def __init__(self,width,height,flag):
        """Initializes Game Object. Arguments - width, height, flag\nwidth: width of the screen\nheight: height of the screen\nflag: FULLSCREEN?"""
        super(Game,self).__init__()
        pygame.init()       # initialize pygame (so that all modules work properly)
        if flag:            # determine FULLSCREEN or not
            self.screen = pygame.display.set_mode((width,height),pygame.FULLSCREEN)
        else:
            self.screen = pygame.display.set_mode((width,height))
        pygame.display.set_caption("Disgruntled Notes") # set the title of the window
        pygame.display.set_icon(pygame.image.load("../media/images/icon.png").convert())    # set the icon for the game
        self.pause = pygame.image.load("../media/images/Pause_Screen.png").convert()        # load the pause screen image into memory

        self.background = pygame.image.load("../media/images/BG1.png").convert_alpha()
        # begin declaring game variables
        self.rect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        # lists of items
        self.bullet_list = []
        self.note_list = []
        self.powerup_list = []
        self.bars = []
        self.rest_list = []
        self.song_list = ["../media/sounds/Cola+Supola.mp3","../media/sounds/RaspberryShortbread.mp3","../media/sounds/NickelCadium.mp3","../media/sounds/JustStartingToBurn.mp3"]
        self.sound_list = [pygame.mixer.Sound("../media/sounds/boss_death.wav"),pygame.mixer.Sound("../media/sounds/boss_pain.wav"),pygame.mixer.Sound("../media/sounds/boss_shoot.wav"),
                           pygame.mixer.Sound("../media/sounds/note_hit.wav"),pygame.mixer.Sound("../media/sounds/player_death.wav"),pygame.mixer.Sound("../media/sounds/player_pain.wav"),
                           pygame.mixer.Sound("../media/sounds/player_shoot.wav"),pygame.mixer.Sound("../media/sounds/powerup_collect.wav"),pygame.mixer.Sound("../media/sounds/roundstart.wav"),
                           pygame.mixer.Sound("../media/sounds/shuffle.wav"),pygame.mixer.Sound("../media/sounds/shotgun_shoot.wav"),pygame.mixer.Sound("../media/sounds/slowdown_off.wav"),
                           pygame.mixer.Sound("../media/sounds/slowdown_on.wav"),pygame.mixer.Sound("../media/sounds/speedup_off.wav"),pygame.mixer.Sound("../media/sounds/speedup_on.wav")]
        # font
        self.font = pygame.font.Font(None,32)
        # game status
        self.status = "menu"
        self.combo = 0
        self.multiplier = 1.0
        self.inkblasts = 0
        # menu item
        self.menu = menu.Menu()
        # level information
        self.level = 0
        self.frame_counter = 0 # note generation
        self.new_level = False
        self.level_counter = 0
        self.movement_counter =250
        self.movement_counter = 250
        self.movement_wait = 0
        random.seed(time.localtime())
        self.time_passed = 0
        self.GOD_MODE = False
        # time manipulation
        self.beat = False
        self.speedy = False
        self.time_dilation = 0
        self.count_s = 0 # speedy count variable
        self.count = 0 # slow count variable
        # overheating variables
        self.time = 1.0
        self.overheat = 0
        self.cooldown = False
        # initializing
        for x in range(5):
            self.bars.append(barlines.barlines(250,x*100+100))
        self.clef = player.Player(self.bars[3])
        self.levelEvents=[]
        self.levelEvents.append(level.Level())
        self.readLevels("./timeEvents.txt")
        self.totalTime=0.0
        self.the_boss = boss.Boss(self.time,self.totalTime)
        self.boss_display = False
        self.current_song= None
        self.credits= pygame.image.load("../media/images/credits.png")
        self.howto=pygame.image.load("../media/images/howto.png")
        #2nd player
        self.overheat2=0
        self.cooldown2=False
        self.bullet_list2=[]
        self.inkblasts2 =0
        self.bars2=[]
        for x in range(5):
            self.bars2.append(barlines.barlines(250,x*100+100+420))
        self.score2=0
        self.bass= player.Player(self.bars2[3])
        self.bass.rect.centery -= 150
        self.bass.sprite_sheet = pygame.image.load("../media/images/Player2.png").convert_alpha()
        self.note_list2=[]
        self.rest_list2 = []
        self.combo2 = 0
        self.inkblasts2=0
        self.playedDoubles=False
        pygame.mixer.music.load(self.song_list[self.level])
        pygame.mixer.music.play(-1)
    def process_events(self):
        """Processes events for the game"""
        if pygame.mixer.music.get_pos() == -1 and self.status == "game" and self.new_level == False and self.playedDoubles == False:
            self.level += 1
            self.new_level = True
        if self.playedDoubles == True and pygame.mixer.music.get_pos() == -1:
            self.status = "win"
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit(0)
                if event.key == pygame.K_g:
                    self.GOD_MODE = True
                if self.status == "game" or self.status == "paused" or self.status == "double":
                    if event.key == pygame.K_p:
                        if self.status == "paused":
                            pygame.mixer.music.unpause()
                            if self.playedDoubles == True:
                                self.status = "double"
                            else:
                                self.status = "game"
                        elif self.status == "game" or self.status == "double":
                            pygame.mixer.music.pause()
                            self.status = "paused"
                    if self.status == "paused":
                        if event.key == pygame.K_q:
                            pygame.mixer.music.stop()
                            self.status = "menu"
                            self.level = 0
                            self.screen = pygame.display.set_mode((1200,600))
                            pygame.mixer.music.load(self.song_list[0])
                            pygame.mixer.music.play(-1)
                            self.playedDoubles = False
                    if self.status != "paused":
                        if event.key == pygame.K_UP or event.key == pygame.K_w:
                            self.clef.move = -50
                        elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                            self.clef.move = 50
                        if event.key == pygame.K_LSHIFT:
                            if self.cooldown == False:
                                self.sound_list[6].play()
                                self.bullet_list.append(bullet.Bullet(self.clef.rect.centery,self.bars[self.clef.rect.centerx/50]))
                                self.overheat += 20
                                self.clef.throw()
                                if self.clef.score - 10 < 0:
                                    self.clef.score = 0
                                else:
                                    self.clef.score -= 10
                        if event.key == pygame.K_x and self.new_level == False:
                            if self.beat == True:
                                self.beat = False
                                self.time = 1.0
                                break
                            if self.time_dilation > 0:
                                self.beat = True
                            if self.count%120 == 0 and self.count != 0:
                                self.speedy = False
                                self.time_dilation -= 1
                        if event.key == pygame.K_z:
                            if self.inkblasts > 0:
                                self.inkblasts -= 1
                                for x in range(-1,2):
                                    if (self.clef.rect.centery+(x*50) > 500) or (self.clef.rect.centery+(x*50) < 100):
                                        continue
                                    else:
                                        self.bullet_list.append(bullet.Bullet(self.clef.rect.centery+(x*50),self.bars[self.clef.rect.centerx/50]))
                        if self.status == "double":
                            self.playedDoubles = True
                        #2nd player
                            if event.key == pygame.K_o:
                                self.bass.move = -50
                            elif event.key == pygame.K_l:
                                self.bass.move = 50
                            if event.key == pygame.K_RSHIFT:
                                if self.cooldown2 == False:
                                    self.bullet_list2.append(bullet.Bullet(self.bass.rect.centery,self.bars2[self.bass.rect.centerx/50]))
                                    self.overheat2 += 25
                                    self.bass.throw()
                                    if self.bass.score - 10 < 0:
                                        self.bass.score = 0
                                    else:
                                        self.bass.score -= 10
                            if event.key == pygame.K_SLASH:
                                if self.inkblasts2 > 0:
                                    self.inkblasts2 -= 1
                                    for x in range(-1,2):
                                        if (self.bass.rect.centery+(x*50) > 500) or (self.bass.rect.centery+(x*50) < 100):
                                            continue
                                        else:
                                            self.bullet_list2.append(bullet.Bullet(self.bass.rect.centery+(x*50),self.bars2[self.bass.rect.centerx/50]))
                if self.status == "menu":
                    if event.key == pygame.K_UP:
                        self.menu.status = (self.menu.status + 4)%5
                    if event.key == pygame.K_DOWN:
                        self.menu.status = (self.menu.status + 1)%5
                    if event.key == pygame.K_RETURN:
                        if self.menu.status == 0:
                            self.reset()
                            self.status = "game"
                            self.new_level = True
                        if self.menu.status == 4:
                            sys.exit(0)
                        elif self.menu.status == 1:
                            self.status = "double"
                            self.screen = pygame.display.set_mode((1200,1000))
                            self.reset()
                            self.new_level=True
                            self.playedDoubles = True
                            self.level = 1
                        elif self.menu.status==3:
                            self.status= "credits"
                        elif self.menu.status==2:
                            self.status= "howto"
                elif self.status == "howto" or self.status=="credits":
                    if  event.key == pygame.K_RETURN:
                        self.status = "menu"
                        self.playedDoubles = False
                if self.status == "lose" or self.status == "win":
                    if event.key == pygame.K_RETURN:
                        self.status = "menu"
                        self.clef.score = 0
                        self.playedDoubles = False
                        self.screen = pygame.display.set_mode((1200,600))
            if event.type == pygame.QUIT:
                sys.exit(0)
    def update(self):
        """Updates objects in game object"""
        "timed events"
        self.totalTime+=self.clock.get_time()
        events=self.levelEvents[self.level].check(self.totalTime)
        if events.name == "wave1":
            self.bars[0].thump()
        elif events.name == "wave2":
            self.bars[1].thump()
        elif events.name == "wave3":
            self.bars[2].thump()
        elif events.name == "next":
            self.new_level=True
            if self.level!=3:
                self.level+=1
            else:
                pass #winning conditon
        elif events.name == "boss" or self.time_passed > 55000:
            self.boss_display = True
        if self.status == "double":
            if self.frame_counter%(30/(self.level+1)) == 0:
                random_number =(((random.random()*9)//1)%9)+2
                if len(self.note_list) < 10:
                    another_random = random.random()*3
                    if another_random < 0.025:
                        pass
                    if another_random < 1:
                        pass
                    elif another_random < 2:
                        pass
                    else:
                        self.note_list.append(note.Note((random_number*50)-10,self.bars[int((random_number-2)/2)],random_number-2))
                        self.note_list2.append(note.Note(((random_number*50)-10)+420,self.bars2[int((random_number-2)/2)],random_number-2))
        elif events.name == "endsong":
            if self.status=="double":
                self.status="win"
        if self.clef.health == 0 and self.status == "game":
            self.menu.update_highscore(self.clef.score)
            self.status = "lose"
            self.clef.health = 100
            self.level=0
            self.reset()
        if self.status  == "game" or self.status=="double":
            self.time_passed += 1
            self.overheat -= 1
            if self.overheat < 0:
                self.overheat = 0
                self.cooldown = False
            if self.overheat > 100:
                self.overheat = 100
                self.cooldown = True
            self.clef.update(self.rect)
            self.clef.reset()
            if self.beat==True:
                self.time = .5
                if self.count == self.time_dilation:
                    self.time = 1.0
                    self.beat = False
                    self.count = 0
                    self.time_dilation = 0
                else:
                    self.count += 1
                if self.count == 0:
                    for x in self.bars:
                        x.thump()
            elif self.speedy == True:
                self.time = 2.5
                self.count_s += 1
                if self.count_s > 240:
                    self.time = 1.0
                    self.speedy = False
                    self.count_s = 0
                if self.count_s == 0:
                    for x in self.bars:
                        x.thump()
        if self.status == "double":
            self.overheat2 -= 1
            if self.overheat2 < 0:
                self.overheat2 = 0
                self.cooldown2 = False
            if self.overheat2 > 100:
                self.overheat2 = 100
                self.cooldown2 = True
            self.bass.update(self.rect)
            self.bass.reset()
            for _barlines in self.bars2:
                _barlines.update(self.rect,self.time)
        if self.status == "game" or self.status == "double":
            for _barlines in self.bars:
                _barlines.update(self.rect,self.time)
            self.update_projectiles()
            if self.boss_display == True:
                self.the_boss.update(self.time,self.totalTime)
    def draw(self):
        """Draws objects in game object"""
        self.screen.fill((255,255,255))
        if self.playedDoubles == True:
            new_img = pygame.transform.scale(self.background,(1200,1200))
            self.screen.blit(new_img,new_img.get_rect())
        else:
            self.screen.blit(self.background,self.background.get_rect())
        if self.new_level == True and (self.status == "game" or self.status == "double"):
            self.begin_new_level()
        if self.status == "game":
            for x in self.bars:
                x.set_color((self.overheat/100.0)*255,0,0)
                x.draw(self.screen)
            self.clef.draw(self.screen)
            for bullet in self.bullet_list:
                bullet.draw(self.screen)
            for note in self.note_list:
                note.draw(self.screen)
            for powerup in self.powerup_list:
                powerup.draw(self.screen)
            for rest in self.rest_list:
                rest.draw(self.screen)
            pygame.draw.line(self.screen,((self.overheat/100.0)*255,self.overheat,0),(250,self.bars[0].curveLoc(250)-1),(250,self.bars[4].curveLoc(250)+2),15)
            font_surf = self.font.render("Score: %d"%self.clef.score,1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(0,self.rect.bottom-75,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Health ",1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(0,self.rect.bottom-42,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("InkBlasts: %d"%self.inkblasts,1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(300,self.rect.bottom-75,font_rect.right,font_rect.bottom))
            pygame.draw.rect(self.screen,(255,0,0),pygame.Rect(100,self.rect.bottom-42,self.clef.health,font_rect.bottom),0)
            font_surf = self.font.render("Time Dilation: %.2f seconds"%((self.time_dilation-self.count)/60.0),1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(300,self.rect.bottom-42,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Multiplier: %.1fx"%(self.combo+1),1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(750,self.rect.bottom-75,font_rect.right,font_rect.bottom))
            pygame.draw.rect(self.screen,(255,0,0),pygame.Rect(100,self.rect.bottom-42,self.clef.health,font_rect.bottom),0)
            if self.boss_display == True:
                self.the_boss.draw(self.screen)
        if self.playedDoubles == True and self.status == "double":
            for x in self.bars:
                x.set_color((self.overheat/100.0)*255,0,0)
                x.draw(self.screen)
            for x in self.bars2:
                x.set_color((self.overheat2/100.0)*255,0,0)
                x.draw(self.screen)
            self.bass.draw(self.screen)
            self.clef.draw(self.screen)
            for bullet in self.bullet_list:
                bullet.draw(self.screen)
            for bullet in self.bullet_list2:
                bullet.draw(self.screen)
            for note in self.note_list:
                note.draw(self.screen)
            for note in self.note_list2:
                note.draw(self.screen)
            for powerup in self.powerup_list:
                powerup.draw(self.screen)
            for rest in self.rest_list:
                rest.draw(self.screen)
            for _rest in self.rest_list2:
                _rest.draw(self.screen)
            pygame.draw.line(self.screen,((self.overheat/100.0)*255,self.overheat,0),(250,self.bars[0].curveLoc(250)-1),(250,self.bars[4].curveLoc(250)+2),15)
            pygame.draw.line(self.screen,((self.overheat2/100.0)*255,self.overheat2,0),(250,self.bars2[0].curveLoc(250)-1),(250,self.bars2[4].curveLoc(250)+2),15)
            font_surf = self.font.render("Score 1: %d"%self.clef.score,1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(300,self.rect.top+50,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Score 2: %d"%self.bass.score,1,(0,0,0))
            self.screen.blit(font_surf,pygame.Rect(300,self.rect.bottom+330,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("InkBlasts: %d"%self.inkblasts,1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(550,self.rect.top+50,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("InkBlasts2: %d"%self.inkblasts2,1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(550,self.rect.bottom+330,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Multiplier: %.1fx"%(self.combo+1),1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(1000,self.rect.top+50,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Multiplier: %.1fx"%(self.combo2+1),1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(1000,self.rect.bottom+330,font_rect.right,font_rect.bottom))
            if self.boss_display == True:
                self.the_boss.draw(self.screen)
        if self.status == "paused":
            self.screen.blit(self.pause,pygame.Rect(300,150,600,300))
        if self.status == "menu":
            self.menu.draw(self.screen)
        if self.status == "win":   
            big_font  = pygame.font.Font(None,72)            
            if self.playedDoubles:
                if self.bass.score>self.clef.score:
                    font_surf = big_font.render("Player 2 Wins!",1,(0,0,0))
                elif self.bass.score==self.clef.score:
                    font_surf = big_font.render("Its a Tie!",1,(0,0,0))
                else:
                    font_surf = big_font.render("Player 1 Wins!",1,(0,0,0))
            else:
                font_surf = big_font.render("YOU WIN",1,(0,0,0))

            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,self.rect.centery-font_rect.centery,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Back to Main Menu",1,(200,200,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,500,font_rect.right,font_rect.bottom))
        if self.status == "lose":
            big_font  = pygame.font.Font(None,72)
            font_surf = big_font.render("YOU LOSE",1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,self.rect.centery-font_rect.centery,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Final Score: %d"%self.clef.score,1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,self.rect.centery-font_rect.centery+50,font_rect.right,font_rect.bottom))
            font_surf = self.font.render("Back to Main Menu",1,(200,200,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,500,font_rect.right,font_rect.bottom))
        if self.status == "howto":
            big_font  = pygame.font.Font(None,72)
            font_surf = big_font.render("Things To Know",1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,25,font_rect.right,font_rect.bottom))

            rectA=self.howto.get_rect()
            self.screen.blit(self.howto,rectA)

            font_surf = self.font.render("Back to Main Menu",1,(200,200,0))

            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,500,font_rect.right,font_rect.bottom))
        if self.status == "credits":
            big_font = pygame.font.Font(None,72)
            font_surf = big_font.render("Credits",1,(0,0,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,25,font_rect.right,font_rect.bottom))
            self.screen.blit(self.credits,pygame.Rect(self.rect.centerx-self.credits.get_rect().centerx,100,self.credits.get_rect().right,self.credits.get_rect().bottom))
            font_surf = self.font.render("Back to Main Menu",1,(200,200,0))
            font_rect = font_surf.get_rect()
            self.screen.blit(font_surf,pygame.Rect(self.rect.centerx-font_rect.centerx,500,font_rect.right,font_rect.bottom))
    def reset(self):
        """Resets state variables"""
        self.bullet_list = []
        self.note_list = []
        self.powerup_list = []
        self.rest_list = []
        self.frame_counter = 0
        self.combo = 0
        self.inkblasts = 0
        self.cooldown = False
        self.time = 1.0
        self.count = 0
        self.overheat = 0
        self.beat = False
        self.level = 1
        self.time_dilation = 0
        self.totalTime=0
        self.count_s = 0
        self.speedy = False
        pygame.mixer.music.load(self.song_list[self.level])
        pygame.mixer.music.play()
       # pygame.mixer.music.play()
    def update_projectiles(self):
        """A self-contained method for projectile updating"""
        if self.boss_display == True:
           # print "a"+str(self.boss.blastx)
            if math.fabs(self.the_boss.blastHeight)>1000000:
             #   print "b"
                if math.fabs(self.the_boss.rect.centery-self.clef.rect.centery)<20:
                 #   print "c"
                    if self.GOD_MODE == False:
                        self.clef.take_damage(20)
            if self.the_boss.collided(self.bullet_list):
                if self.the_boss.take_damage(10) == "dead":
                   self.boss_display = False
                   self.status == "win"
        # SINGLE PLAYER #
        for bullet in self.bullet_list:
            bullet.update(self.time)
            if bullet.collided(self.rect):
                self.bullet_list.remove(bullet)
                del bullet
        for _note in self.note_list:
            _note.update(self.time)
            collided_value = _note.collided(self.bullet_list)
            if collided_value == 2:
                if _note.happy == False:
                    if self.GOD_MODE == False:
                        self.clef.take_damage(5)
                pygame.mixer.music.set_volume(150)
                self.combo = 0
                if _note.status == "sad":
                    if self.GOD_MODE == False:
                       self.clef.take_damage(5)
                    self.sound_list[5].play()
                    self.combo = 0
                self.note_list.remove(_note)
                del _note
                continue
            if collided_value == 1:
                self.clef.add_score(_note.get_point_value()*(self.combo+1))
                self.clef.take_damage(-1)
                add=40
                powerup_type = _note.spawn_powerup()
                if powerup_type == 1:
                    self.powerup_list.append(inkblast.InkBlast(_note.get_col_rect().centerx,_note.get_col_rect().centery+add,_note.barPar))
                    self.note_list.remove(_note)
                    del _note
                elif powerup_type == 2 and self.status == "game":
                    self.powerup_list.append(slowtime.SlowTime(_note.get_col_rect().centerx,_note.get_col_rect().centery+add,_note.barPar))
                    self.note_list.remove(_note)
                    del _note
                elif powerup_type == 3 and self.status == "game":
                    self.powerup_list.append(healthpickup.HealthPickup(_note.get_col_rect().centerx,_note.get_col_rect().centery+add,_note.barPar))
                    self.note_list.remove(_note)
                    del _note
                elif powerup_type == 4 and self.status == "game":
                    self.powerup_list.append(speedtime.SpeedTime(_note.get_col_rect().centerx,_note.get_col_rect().centery+add,_note.barPar))
                    self.note_list.remove(_note)
                    del _note
                if self.time == 0.5:
                    self.combo = self.combo +  0.5
                elif self.time == 2.5:
                    self.combo += 2.5
                else:
                    self.combo += 1
        for _rest in self.rest_list:
            _rest.update(self.time)
            collided_value = _rest.collided(self.bullet_list)
            if collided_value == 1:
                self.rest_list.remove(_rest)
                del _rest
            if collided_value == 2:
                self.rest_shakeup()
                self.rest_list.remove(_rest)
                del _rest
        if self.new_level == False:
            if self.frame_counter%(30/(self.level)) == 0:
                random_number =(((random.random()*9)//1)%9)+2
                if len(self.note_list) < 10:
                    another_random = random.random()*3
                    if another_random < 0.025:
                        self.rest_list.append(rest.Rest((random_number*50)-10,self.bars[int((random_number-2)/2)]))
                    if another_random < 1:
                        pass
                    elif another_random < 2:
                        pass
                    else:
                        self.note_list.append(note.Note((random_number*50)-10,self.bars[int((random_number-2)/2)],random_number-2))
            self.frame_counter = (self.frame_counter+61)%60
        for _powerup in self.powerup_list:
            _powerup.update(self.time)
            if _powerup.collided_with(self.clef):
                if isinstance(_powerup,inkblast.InkBlast):
                    self.inkblasts += 1
                elif isinstance(_powerup,healthpickup.HealthPickup):
                    self.clef.take_damage(-50)
                elif isinstance(_powerup,speedtime.SpeedTime):
                    self.speedy = True
                    self.beat = False
                else:
                    self.speedy = False
                    self.count_s = 0
                    self.time_dilation += 120
                self.powerup_list.remove(_powerup)
                del _powerup
                continue
            if _powerup.collided(self.rect):
                self.powerup_list.remove(_powerup)
                del _powerup
        #2nd Player
        if self.status == "double":
            for bullet in self.bullet_list2:
                bullet.update(self.time)
                if bullet.collided(self.rect):
                    self.bullet_list2.remove(bullet)
                    del bullet
            for _note in self.note_list2:
                _note.update(self.time)
                collided_value = _note.collided(self.bullet_list2)
                if collided_value == 2:
                    if _note.status == "sad":
                        if self.GOD_MODE == False:
                            self.bass.take_damage(5)
                    self.combo2 = 0
                    self.note_list2.remove(_note)
                    del _note
                    continue
                if collided_value == 1:
                    self.bass.add_score(_note.get_point_value()*(self.combo2+1))
                    self.bass.take_damage(-1)
                    add=40
                    powerup_type = _note.spawn_powerup()
                    if powerup_type == 1:
                        self.powerup_list.append(inkblast.InkBlast(_note.rect.centerx,_note.rect.centery+add,_note.barPar))
                        self.note_list2.remove(_note)
                        del _note
                    elif powerup_type == 2:
                        pass
                    elif powerup_type == 3:
                        pass
                    elif powerup_type == 4:
                        pass
                    if self.time == 0.5:
                        self.combo2 = self.combo2 +  0.5
                    elif self.time == 2.5:
                        self.combo2 += 2.5
                    else:
                        self.combo2 += 1
            for _rest in self.rest_list2:
                _rest.update(self.time)
                collided_value = _rest.collided(self.bullet_list2)
                if collided_value == 1:
                    self.rest_list2.remove(_rest)
                    del _rest
                if collided_value == 2:
                    _rest_shakeup()
                    self.rest_list2.remove(_rest)
                    del _rest
            if self.new_level == False:
                if self.frame_counter%15 == 0:
                    random_number =(((random.random()*9)//1)%9)+2
                    if len(self.note_list2) < 10:
                        another_random = random.random()*3
                        if another_random < 0.025:
                            self.rest_list2.append(rest.Rest((random_number*50)-10,self.bars2[int((random_number-2)/2)]))
                        if another_random < 1:
                            pass
                        elif another_random < 2:
                            pass
                        else:
                            self.note_list2.append(note.Note((random_number*50)-10,self.bars2[int((random_number-2)/2)],random_number-2))
                self.frame_counter = (self.frame_counter+61)%60
            for _powerup in self.powerup_list:
                _powerup.update(self.time)
                if _powerup.collided_with(self.bass):
                    if isinstance(_powerup,inkblast.InkBlast):
                        self.inkblasts2 += 1
                    elif isinstance(_powerup,healthpickup.HealthPickup):
                        self.bass.take_damage(-50)
                    elif isinstance(_powerup,speedtime.SpeedTime):
                        self.speedy = True
                        self.beat = False
                    else:
                        self.speedy = False
                        self.count_s = 0
                        self.time_dilation += 120
                    self.powerup_list.remove(_powerup)
                    del _powerup
                    continue
                if _powerup.collided(self.rect):
                    self.powerup_list.remove(_powerup)
                    del _powerup
    def rest_shakeup(self):
        for x in self.note_list:
            random_number = (random.random()*5)%5
            x.shuffle_bar(self.bars[int(random_number)])
    def begin_new_level(self):
        if len(self.note_list) > 0:
            pass
        else:
            if pygame.mixer.music.get_pos() == -1:
                pygame.mixer.music.load(self.song_list[self.level])
                if self.level == 3:
                    pygame.mixer.music.play(-1)
                else:
                    self.speedy = False
                    self.time = 1.0
                    pygame.mixer.music.play()
                self.speedy = False
                self.time = 1.0
            if self.level_counter < 120:
                self.level_counter += 1
            elif self.movement_counter > -1 or self.movement_wait < 120:
                font_surf = self.font.render("L",1,(0,0,0))
                font_rect = font_surf.get_rect()
                self.screen.blit(font_surf,pygame.Rect(1000+self.movement_counter,self.bars[0].curveLoc(1000+self.movement_counter),font_rect.right,font_rect.bottom))
                font_surf = self.font.render("e",1,(0,0,0))
                font_rect = font_surf.get_rect()
                self.screen.blit(font_surf,pygame.Rect(1016+self.movement_counter,self.bars[0].curveLoc(1016+self.movement_counter),font_rect.right,font_rect.bottom))
                font_surf = self.font.render("v",1,(0,0,0))
                font_rect = font_surf.get_rect()
                self.screen.blit(font_surf,pygame.Rect(1032+self.movement_counter,self.bars[0].curveLoc(1032+self.movement_counter),font_rect.right,font_rect.bottom))
                font_surf = self.font.render("e",1,(0,0,0))
                font_rect = font_surf.get_rect()
                self.screen.blit(font_surf,pygame.Rect(1048+self.movement_counter,self.bars[0].curveLoc(1048+self.movement_counter),font_rect.right,font_rect.bottom))
                font_surf = self.font.render("l",1,(0,0,0))
                font_rect = font_surf.get_rect()
                self.screen.blit(font_surf,pygame.Rect(1064+self.movement_counter,self.bars[0].curveLoc(1064+self.movement_counter),font_rect.right,font_rect.bottom))
                font_surf = self.font.render("%d"%self.level,1,(0,0,0))
                font_rect = font_surf.get_rect()
                self.screen.blit(font_surf,pygame.Rect(1096+self.movement_counter,self.bars[0].curveLoc(1096+self.movement_counter),font_rect.right,font_rect.bottom))
                self.movement_counter -= 4
                if self.movement_counter <= -1:
                    self.movement_counter = 0
            if self.movement_counter == 0:
                self.movement_wait += 1
                if self.movement_wait == 90:
                    for x in self.bars:
                        x.thump()
            if self.movement_wait == 120:
                self.new_level = False
                self.movement_counter = 250
                self.movement_wait = 0
                self.level_counter = 0
    def readLevels(self,source):
        fileDump= open(source, 'r')
        alevel=level.Level()
        for line in fileDump:
            line = string.rstrip(line)
            if line == "endlevel":
                if alevel.music!="notdone":
                    self.levelEvents.append(alevel)
                    alevel=level.Level()
            else:
                parse=line.split(" ")
                if parse[0]=="music":
                    alevel.music=parse[1]
                    alevel.musicTime=parse[2]
                elif parse[0]=="event":
                    alevel.addEvent(parse[1],parse[2])